<template>
	<view class="content">
		<text>用户名</text>
		<input type="text" v-model="userName" />
		<text>年龄</text>
		<input type="text" v-model="age" />
		<button type="default" @click="_sendUserInfo">提交表单</button>
		<view class="list-wrapper">
			<view class="list" v-for="(item,index) in userList" :key="index">
				<text>{{item.userName}}</text>
				<button type="default" @click="_deleteUser(item._id)" size="mini">删除</button>
			</view>
		</view>
		<view class="add-img">
			<button type="default" @click="_addImg()" size="mini">上传图片 </button>
			
		</view>
	</view>
</template>

<script>
	export default {
		onLoad() {
			this._getUserInfo();
		},
		data(){
			return{
				userName:"",
				age:"",
				userList:null
			}
		},
		methods:{
			async _sendUserInfo(){
				const res = await uniCloud.callFunction({
					name:"add_user",
					data:{
						userName:this.userName,
						age:this.age
					}
				})
			},
			async  _getUserInfo(){
				const res = await uniCloud.callFunction({
					name:"get_user",
					
				})
				this.userList = res.result.data
				console.log(this.userList);
			},
			async _deleteUser(id){
				const res = await uniCloud.callFunction({
					name:"del_user",
					data:{
						id
					}
				})
			},
			async _addImg(){
				uni.chooseImage({
					count:1,
					async success(file) {
						let result = await uniCloud.uploadFile({
							cloudPath:"deleteFile.jpg",
							filePath:file.tempFilePaths[0],
							success(res) {
								console.log(res);
								
							}
						})
					}
				})
			}
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}
	.list-wrapper{
		width: 100%;
	}
	.list{
		display: flex;
		justify-content: space-between;
		align-items: center;
	}
</style>
